//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgUrls: [
    ],
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    firstArticleId:0,
    firstArticleImg:"",
    firstArticleTitle: "",
    firstArticleContent:"",
    articleRecommends:[],
    audioRecommends:[]
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  onReady:function(e){
      var _this = this;
      createBanners(_this);
    createArticlesAndAudios(_this);
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }, bindToArticleDetail: function(e){
    var id = e.currentTarget.id;

    var detailObj = { url: "/pages/article_detail/article_detail?id=" + id };
    wx.navigateTo(detailObj);
  }
});


function createArticlesAndAudios(_this){

  var reqData = {
    request:
      {
      }
  };

  wx.request({
    url: app.globalData.requestUrl + '/recommend/list',
    data: JSON.stringify(reqData),
    header: {},
    method: 'POST',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      if (res.data.responseCode === "000") {

        var articleRecommends = res.data.data.articleRecommends;
        
        var curArticleRecommends = getCurArticleRecommends(articleRecommends);

        var curAudioRecommends = getCurAudioRecommends(res.data.data.audioRecommends);

        var _data = {
          firstArticleImg: res.data.data.firstArticleImg, firstArticleContent: res.data.data.firstArticleContent, firstArticleTitle: articleRecommends[0].item.title, firstArticleId: articleRecommends[0].item.id, articleRecommends: curArticleRecommends, audioRecommends:curAudioRecommends
        };

        _this.setData(_data);
      }

    },
    fail: function (res) { },
    complete: function (res) {
    },
  });
}

function getCurAudioRecommends(audioRecommends){
  var curAudioRecommends = [];
  for (var index in audioRecommends) {
    var item = audioRecommends[index].item;
    var curAudioRecommend = { id: item.id, title: item.title, url: item.playTypes[0].url, coverUrl: item.coverUrl};

    curAudioRecommends.push(curAudioRecommend);
  }
  return curAudioRecommends;
}


function getCurArticleRecommends(articleRecommends){
  var curArticleRecommends = [];
  for(var index in articleRecommends){
    if(index == 0){
      continue;
    }
    var item = articleRecommends[index].item;
    var hasCover = false;
    if(item.coverUrl){
      hasCover = true;
    }

    var curArticleRecommend = {id:item.id,title:item.title,summary:item.summary,coverUrl:item.coverUrl,
      hasCover:hasCover};

    curArticleRecommends.push(curArticleRecommend);

  }

  return curArticleRecommends;
}

function createBanners(_this){

  var reqData = {
    request:
      {
      }
  };

  wx.request({
    url: app.globalData.requestUrl + '/banner/list',
    data: JSON.stringify(reqData),
    header: {},
    method: 'POST',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      if (res.data.responseCode === "000") {
        var items = res.data.data.items;
        var curImgUrls = new Array();
        for(var index in items){
          curImgUrls.push(items[index].coverUrl);
        }
        _this.setData({ imgUrls: curImgUrls });
      }

    },
    fail: function (res) { },
    complete: function (res) {
    },
  });
}
